package com.neusoft.provider.controller;

import com.neusoft.common.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private DiscoveryClient client;

    @RequestMapping("/findStudent.do")
    public Student getStudent()
    {
        return new Student(2,"李四","1998-02-23","湖北武汉");
    }

    @RequestMapping("/findStu.do/{id}")
    public Student getStudent(@PathVariable("id") Integer id)
    {
        if(null!=id)
            return new Student(2,"李四","1998-02-23","湖北武汉");
        else
            return null;
    }

    @RequestMapping(value = "/user/discovery", method = RequestMethod.GET)
    public Object discovery()
    {
        List<String> list = client.getServices();
        System.out.println("**********" + list);

        List<ServiceInstance> srvList = client.getInstances("EUREKA-CLIENT-USER");
        for (ServiceInstance element : srvList) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
                    + element.getUri());
        }
        return this.client;
    }

}
